package sz.szsmk.citizencard.ui.activity;


import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chenenyu.router.annotation.Route;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.NetSearchActivityBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.bean.response.PrepayResponse;
import sz.szsmk.citizencard.model.network.bean.response.ShopListRes;
import sz.szsmk.citizencard.mvp.contract.INetSerchContact;
import sz.szsmk.citizencard.mvp.presenter.NetServicePresenter;
import sz.szsmk.citizencard.ui.adapter.BusinessAdapter;
import sz.szsmk.citizencard.ui.adapter.NetSearchAdapter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/9/9 23:20
 * description:服务网点/消费商家搜索结果
 */
@Route("netsearch")
public class NetSearchActivity extends IBaseActivity<NetServicePresenter, NetSearchActivityBinding>
        implements INetSerchContact.INetSerchView {

    private NetSearchAdapter netSearchAdapter;

    private List<ShopListRes.ShopListInfosBean> infoBeans = new ArrayList<>();
    private List<PrepayResponse.WebsiteListInfoBean> netBean = new ArrayList<>();

    private int currentPager = 1;

    private String keyWord = "";
    private String door;

    @Override
    protected int attachLayoutRes() {
        return R.layout.net_search_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        dataBinding.titleLayout.settingTitle.setText("搜索结果");
        dataBinding.titleLayout.back.setOnClickListener(v -> finish());
        keyWord = getIntent().getStringExtra("word");
        door = getIntent().getStringExtra("door");

        netSearchAdapter = new NetSearchAdapter();

        dataBinding.contentList.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.contentList.setAdapter(netSearchAdapter);
        netSearchAdapter.setKey(door);
        netSearchAdapter.setEmptyView(R.layout.layout_nodata,dataBinding.contentList);


        dataBinding.swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPager = 1;
                if (door.equals("net")) {
                    //网点
                    mPresenter.sernetSeatch(keyWord, currentPager);
                } else {
                    //商家
                }
            }
        });
        dataBinding.swipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (door.equals("net")) {
                    //网点
                    mPresenter.sernetSeatch(keyWord, currentPager);
                } else {
                    //商家
                }
            }
        });


        if (door.equals("net")) {
            //网点
            mPresenter.sernetSeatch(keyWord, currentPager);
        } else {
        }

    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void shopSearchFail() {
        dataBinding.swipeRefreshLayout.closeHeaderOrFooter();
    }

    @Override
    public void shopSearchSucc(List<ShopListRes.ShopListInfosBean> shopListInfos) {
        if(currentPager==1){
            netSearchAdapter.setNewData(shopListInfos);
        }else{
            netSearchAdapter.addData(shopListInfos);
        }

        boolean hasMore = shopListInfos.size() == 15;
        if (hasMore){
            currentPager++;
        }else{
            //完成加载并标记没有更多数据
            dataBinding.swipeRefreshLayout.finishLoadMoreWithNoMoreData();
        }
        dataBinding.swipeRefreshLayout.closeHeaderOrFooter();
    }

}
